<?php

class installWidgetSectionTask extends sfBaseTask
{
  protected function configure()
  {

    $this->addOptions(array(
      new sfCommandOption('section', null, sfCommandOption::PARAMETER_REQUIRED, 'The section name'),
      new sfCommandOption('widget', null, sfCommandOption::PARAMETER_REQUIRED, 'The widget name')
    ));

    $this->namespace        = 'wixet';
    $this->name             = 'install-widget-section';
    $this->briefDescription = 'Install a widget into a section';
    
  }

  protected function execute($arguments = array(), $options = array())
  {
    // initialize the database connection
    $databaseManager = new sfDatabaseManager($this->configuration);
    $sections = Doctrine::getTable('Section');
	$sections = $sections->findByDql('name = ?', $options['section']);
	$section = $sections->getFirst();

	$sectionId = $section->getId();
	$widgets = Doctrine::getTable('Widget');
	$widgets = $widgets->findByDql('name = ?', $options['widget']);
	$widget = $widgets->getFirst();
	
	$position = new SectionWidget();
	if(is_object($widget))
    {
		$positions = Doctrine::getTable('SectionWidget');
		$positions = $positions->findByDql('section_id = ?', $section->getId());
		$rowPosition = $positions->count();
		//$this->log("Position es ".$rowPosition);
		$position->setPosition($rowPosition);
	}
	else
		echo 'Section "'.$options['section'].'" does not exist';
	
	$position->setSectionId($section->getId());
	$position->setWidgetId($widget->getWidgetId());
	$position->save();
    
    $this->log("Widget installed");
  }
}
